package com.xiyili.osc.ui.news;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.oschina.app.AppException;
import net.oschina.app.bean.News;
import net.oschina.app.bean.NewsList;
import net.oschina.app.common.PagingInfo;
import net.oschina.app.common.UIHelper;
import net.oschina.app.requests.NewsListRequest;
import net.oschina.app.ui.ToastUtils;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.xiyili.osc.G;
import com.xiyili.osc.R;

public class NewsListFragment extends Fragment implements OnItemClickListener {
	private static final String TAG = "NewsListFragment";

	private ListView mListView;
	private NewsListAdapter mListAdapter;
	
	private final List<News> mNewsList = new ArrayList<News>();
	private int curNewsCatalog = NewsList.CATALOG_ALL;
	
	private RequestQueue _requestQueue;
	
	public NewsListFragment(){
		// for supper class
	}
	
	public static NewsListFragment newInstance(int catalog){
		NewsListFragment f = new NewsListFragment();
		Bundle args = new Bundle();
		args.putInt("catalog", catalog);
		f.setArguments(args);
		return f;
	}
	
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Bundle args = getArguments();
		if(args != null){
			curNewsCatalog = args.getInt("catalog");
		}
		Log.d(TAG, ">>>onCreate");
	}
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		
		Log.d(TAG, "onAttach>>>");
		
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		Log.d(TAG, "onCreateView>>>");
		View layout  =  inflater.inflate(R.layout.news_v2_list, container, false);
		mListAdapter = new NewsListAdapter(getActivity(), mNewsList);
		mListView = (ListView) layout.findViewById(R.id.list);
		mListView.setAdapter(mListAdapter);
		mListView.setOnItemClickListener(this);
		Log.d(TAG, "onCreateView>>>   2");
		
		loadNewsListData(curNewsCatalog, 0, UIHelper.LISTVIEW_ACTION_REFRESH);
		return layout;
	}

	/**
	 * 线程加载新闻数据
	 * 
	 * @param catalog
	 *            分类
	 * @param pageIndex
	 *            当前页数
	 * @param handler
	 *            处理器
	 * @param action
	 *            动作标识
	 */
	private void loadNewsListData(final int catalog, final int pageIndex, final int action) {
		//mHeadProgress.setVisibility(ProgressBar.VISIBLE);
		Log.d(TAG, "loadNewsListData>>>");
		
		PagingInfo pi = new PagingInfo(pageIndex, catalog);
		
		NewsListRequest request = new NewsListRequest(pi, new Response.Listener<String>() {

			@Override
			public void onResponse(String response) {
				try {
					NewsList list =  NewsList.parse(response);
					mNewsList.clear();
					mNewsList.addAll(list.getNewslist());
					mListAdapter.notifyDataSetChanged();
					
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (AppException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			}
		}, new Response.ErrorListener() {

			@Override
			public void onErrorResponse(VolleyError error) {
				Log.e(TAG, "onErrorResponse#"+error);
				ToastUtils.showFailure(getActivity(), "error:"+error);
			}
		});
		_requestQueue = G.getRequestQueue(getActivity());
		_requestQueue.add(request);
	}


	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		News news = mNewsList.get(position);
		Log.d(TAG, "onItemClick#news="+news);
		Intent intent = new Intent(getActivity(), NewsDetailActivity.class);
		intent.putExtra("news_id", news.getId());
		intent.putExtra("news_title", news.getTitle());
		intent.putExtra("news_author", news.getAuthor());
		intent.putExtra("news_pubDate", news.getPubDate());
		intent.putExtra("news_comment_count", news.getCommentCount());
		getActivity().startActivity(intent);
		
	}


}
